package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import pojo.Customer;
import service.CustomerManager;

public class CustomerManagerImpl implements CustomerManager{

	private Map<String, Customer> cMap = new ConcurrentHashMap<String,Customer>(); 
	
	@Override
	public Customer addCustomer(Customer c) {
		c.setId(UUID.randomUUID().toString());
		this.cMap.put(c.getId(), c);
		return c;
	}

	@Override
	public List<Customer> findAllCustomers() {
		Set<String> set  = cMap.keySet();
		Iterator<String> it = set.iterator();
		
		List<Customer> list = new ArrayList<Customer>();
		while(it.hasNext()) {
			String cid = it.next();
			list.add(this.cMap.get(cid));
		}
		return list;
	}

	@Override
	public Customer updateCustomer(Customer c) {
		if(!this.cMap.containsKey(c.getId())) {
			return null;
		}else {
			this.cMap.put(c.getId(), c);
			return c;
		}
	}

	@Override
	public Customer deleteCustomer(String cid) {
		if(!this.cMap.containsKey(cid)) {
			return null;
		}else {
			return  this.cMap.remove(cid);
		}
	}

}
